<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="addPermissionFn(1,'0')">添加权限</el-button>
        </div>

        <el-table border :data="permissionList" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template v-slot="scope">
              <el-button v-if="scope.row.type === 1" type="text" @click="addPermissionFn(2, scope.row.id)">添加</el-button>
              <el-button type="text" @click="edit(scope.row.id)">编辑</el-button>
              <el-button type="text" @click="delFn(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>

      <!-- 弹窗 -->
      <PerDialog ref="perDialogRef" :show-dialog.sync="showPerDialog" @confirmEV="confirmFn" />
    </div>
  </div>
</template>

<script>
import { getPermissionListAPI, addPermissionAPI, getPermissionDetailAPI, updatePermissionAPI, delPermissionAPI } from '@/api'
import { transTree } from '@/utils'
import PerDialog from './components/perDialog.vue'
export default {
  components: {
    PerDialog
  },
  data() {
    return {
      permissionList: [], // 权限管理列表数据
      showPerDialog: false, // 控制新增-权限点-弹窗是否出现
      type: 0, // 添加权限-类型
      pid: '', // 添加权限-pid
      isEdit: false // 判断添加/编辑
    }
  },
  created() {
    // 调用获取权限管理列表的数据
    this.getPermissionList()
  },
  methods: {
    // 获取权限管理列表的数据
    async getPermissionList() {
      const res = await getPermissionListAPI().catch(err => err)
      if (!res.success) return this.$message.error(res.message)
      this.permissionList = transTree(res.data, '0')
    },

    // 添加权限-按钮点击事件-弹窗出现
    addPermissionFn(type, pid) {
      this.isEdit = false
      this.showPerDialog = true
      this.type = type
      this.pid = pid
    },

    // 添加权限-接口调用(弹框点击确定)
    async confirmFn(dataObj) {
      if (this.isEdit) {
        const res = await updatePermissionAPI(dataObj)
        if (!res.success) return this.$message.error(res.message)
        this.$message.success(res.message)
      } else {
        const res = await addPermissionAPI({ ...dataObj, type: this.type, pid: this.pid })
        if (!res.success) return this.$message.error(res.message)
        this.$message.success(res.message)
      }

      // 调用获取权限管理列表的数据
      this.getPermissionList()
    },

    // 编辑权限点 => 点击事件
    async edit(id) {
      this.isEdit = true
      this.showPerDialog = true
      // 请求详情信息
      const res = await getPermissionDetailAPI(id)
      this.$refs.perDialogRef.formData = res.data
    },

    // 删除权限点 => 点击事件
    async delFn(id) {
      // 确认消息
      const confirmRes = await this.$confirm('此操作将永久删除该权限, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)

      if (confirmRes === 'cancel') return this.$message.info('您取消了删除')

      // 调用接口，并传入 ID
      const res = await delPermissionAPI(id)
      if (!res.success) return this.$message.error(res.message)
      this.$message.success(res.message)
      this.getPermissionList()
    }
  }
}
</script>

<style lang="scss" scoped></style>
